using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	/// <summary>
	/// Author: Zhang Andy
	/// </summary>
	public class SVNMerge : CommandBase
	{
        /// <summary>
        /// We have our variables declared here.
        /// Access modifiers as you can observe, is public. Why?
        /// Because we trust our programmers.
        /// 
        /// Try to be verbose with the variable names.
        /// Also, try to include *sane defaults*. Take peg_revision for eg.
        /// </summary>
        private java.io.File path1;
        private SVNRevision revision1;
        private SVNURL path2;
        private SVNRevision revision2;
        private java.io.File dstPath;
        private SVNDepth depth = SVNDepth.INFINITY; 
        private bool useAncestry = true;
        private bool force = true;
        private bool dryRun = false;
        private bool recordOnly = false;

        /// <summary>
        /// Strings Constructor
        /// </summary>
        public SVNMerge(string path1, string rev1, string path2, string rev2)
        {
            this.path1 = new java.io.File(path1);
            this.revision1 = SVNRevision.parse(rev1);
            this.path2 = SVNURL.parseURIDecoded(path2);
            this.revision2 = SVNRevision.parse(rev2);
            this.dstPath = this.path1;
        }

        public override void Execute()
		{
            //DEBUG MSG. *Always* include this.
            Debug.WriteLine("SVN Merge Execution");

            //library setup
            Tools.SetupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();
            SVNDiffClient dc = cm.getDiffClient();

            try
            {
                dc.doMerge(this.path1,this.revision1,this.path2,this.revision2,this.dstPath,this.depth,this.useAncestry,this.force,this.dryRun,this.recordOnly);
                
            }
            catch (SVNException e)
            {
                //we convert this exception to a non SVNKit exception.
                //we want to minimize interfacing with SVNkit outside of this namespace (abstraction)
                throw new SVNExecuteException(e.getMessage());
            }
		}
	}
}
